package com.wangsh.userscenter.client.interceptor ; 

import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wangsh.userscenter.common.controller.BaseController;
import com.wangsh.userscenter.common.util.ConstatFinalUtil;
import com.wangsh.userscenter.common.util.HTTPUtil;
import com.wangsh.userscenter.service.users.pojo.AAdmins;
import com.wangsh.userscenter.service.users.pojo.AUsers;

/**
 * 拦截器
 * @author wangsh_sxt
 *
 */
@Component("adminsInterceptor")
public class AdminsInterceptor extends BaseController implements HandlerInterceptor
{
	/**
	 * 执行方法之前
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
	{
		ResourceBundle bundle = ResourceBundle.getBundle("client");
		/*ConstatFinalUtil.SYS_LOG.info("执行方法之前拦截:obj:{}",handler);*/
		HttpSession session = request.getSession();
		String currUrl = request.getParameter("returnUrl");
		if(currUrl == null)
		{
			currUrl = ""; 
		}
		
		if("".equalsIgnoreCase(currUrl))
		{
			currUrl = request.getRequestURL() + ""; 
			String queryStr = request.getQueryString() ; 
			if(queryStr != null && !"".equalsIgnoreCase(queryStr))
			{
				currUrl += "?" + queryStr ; 
			}
		}
		
		/* 获取用户对象 */
		AAdmins admins = (AAdmins) this.findObjfromSession(request, "2");
		if(admins != null)
		{
			return true;
		}
		
		/* 木有登陆,跳转到用户中心 */
		String ticket = request.getParameter("ticket");
		if(ticket == null)
		{
			ticket = "" ; 
		}
		
		try
		{
			HTTPUtil httpUtil = new HTTPUtil();
			Map<String,String> paramsMap = new HashMap<String,String>();
			paramsMap.put("requestURL", bundle.getString("sso.admins.verify.ticket"));
			paramsMap.put("Referer", currUrl);
			paramsMap.put("ticket", ticket);
			String respStr = httpUtil.methodPost(Collections.EMPTY_MAP, paramsMap);
			JSONObject responseJOSN = JSON.parseObject(respStr);
			if("0".equalsIgnoreCase(responseJOSN.getString("code")))
			{
				JSONObject dataJSON = responseJOSN.getJSONObject("data");
				if(dataJSON.get("users") != null)
				{
					//请求成功
					/* 查询数据,如果存在,更新数据库
					 * 如果不存在,向数据库中插入一条记录 */
					AAdmins adminsRes = new AAdmins() ; 
					adminsRes.setTicketStr(responseJOSN.getString("ticket"));
					ConstatFinalUtil.SYS_LOG.info("请求对方服务器返回结果:{}" , responseJOSN);
					session.setAttribute("users", adminsRes);
					return true ; 
				}
			}
		} catch (Exception e)
		{
		}
		
		/* 跳转到用户中心,登陆 */
		String ssoMainUrl = bundle.getString("sso.admins.login");
		ssoMainUrl += "?returnUrl=" + URLEncoder.encode(currUrl, "UTF-8") ; 
		response.sendRedirect(ssoMainUrl);
		
		return false ;
	}
	
	/**
	 * 
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception
	{
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 执行方法之后
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception
	{
		// TODO Auto-generated method stub
		
	}

}
